//
//  WBPopMenuView.m
//  writingDemo
//
//  Created by 周佳兴 on 16/3/17.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "WBPopMenuView.h"
#import "WBTableViewDataSource.h"
#import "WBTableViewDelegate.h"
#import "WBTableViewCell.h"
#import "WBPopMenuModel.h"
#import "WBPopMenuSingleton.h"
#import "Masonry.h"

#define WBNUMBER 6

@interface WBPopMenuView ()
@property (nonatomic, strong) WBTableViewDataSource * tableViewDataSource;
@property (nonatomic, strong) WBTableViewDelegate   * tableViewDelegate;
@end

@implementation WBPopMenuView

- (instancetype) init {
    if (self = [super init]) {
        
    }
    return self;
}

- (instancetype) initWithFrame:(CGRect)frame
                     menuWidth:(CGFloat)menuWidth
                         items:(NSArray *)items
                        action:(void(^)(NSInteger index))action {
    if (self = [super initWithFrame:frame]) {
        self.menuWidth = menuWidth;
        self.menuItem = items;
        self.action = [action copy];
        
        self.tableViewDataSource = [[WBTableViewDataSource alloc]initWithItems:items cellClass:[WBTableViewCell class] configureCellBlock:^(WBTableViewCell *cell, WBPopMenuModel *model) {
            WBTableViewCell * tableViewCell = (WBTableViewCell *)cell;
            //            tableViewCell.textLabel.text = model.title;
            //            tableViewCell.imageView.image = [UIImage imageNamed:model.image];
            
            tableViewCell.titleLabel.text = model.title;
            tableViewCell.centerImageView.image = [UIImage imageNamed:model.image];
            
            
        }];
        self.tableViewDelegate = [[WBTableViewDelegate alloc]initWithDidSelectRowAtIndexPath:^(NSInteger indexRow) {
            if (self.action) {
                self.action(indexRow);
            }
        }];
        NSLog(@"%@",NSStringFromCGRect(self.bounds));
        NSLog(@"%@",NSStringFromCGRect(frame));
        NSLog(@"%@",NSStringFromCGRect([UIScreen mainScreen].bounds));
        NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
        
        self.tableView = [[UITableView alloc]initWithFrame:[self menuFrame:frame] style:UITableViewStylePlain];
        NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
        self.tableView.dataSource = self.tableViewDataSource;
        NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
        self.tableView.delegate   = self.tableViewDelegate;
        NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
        self.tableView.layer.cornerRadius = 10.0f;
        NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
        
        
        
        
        
        
#pragma mark - tableView的背景颜色变成透明
        self.tableView.backgroundColor = [UIColor clearColor];
        
        
        
        self.tableView.layer.anchorPoint = CGPointMake(0.5, 0);//这里开始变化

        
        self.tableView.rowHeight = 40;
        //        NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
        
        
        UIView *v=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        v.backgroundColor=[UIColor yellowColor];

        v.layer.cornerRadius=10;
        v.layer.shadowColor=[UIColor redColor].CGColor;
        v.layer.shadowOffset=CGSizeMake(10, 10);
        v.layer.shadowOpacity=0.5;
        v.layer.shadowRadius=5;
        v.frame = self.tableView.frame;
        
        NSLog(@"%@",NSStringFromCGRect(v.frame));
        
        [self addSubview:self.tableView];
        //        NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
        
        //        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        //            NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
        //            [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        //        }
        //        if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        //            NSLog(@"%@",NSStringFromCGRect(self.tableView.frame));
        //            [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        //        }
        
    }
    return self;
}

- (CGRect)menuFrame:(CGRect)frame {
    CGFloat menuX = [UIScreen mainScreen].bounds.size.width/2-self.menuWidth/2;
    CGFloat menuY = 69;
    CGFloat width = self.menuWidth;
    //    CGFloat heigh = 40 * self.menuItem.count;
    CGFloat heigh = 40 * 0;
    return (CGRect){menuX,menuY,width,heigh};
}

#pragma mark 绘制三角形
- (void)drawRect:(CGRect)rect

{
    // 设置背景色
    [[UIColor whiteColor] set];
    //拿到当前视图准备好的画板
    
    CGContextRef  context = UIGraphicsGetCurrentContext();
    
    //利用path进行绘制三角形
    
    CGContextBeginPath(context);//标记
    CGFloat location = [UIScreen mainScreen].bounds.size.width/2;
    CGContextMoveToPoint(context,location  +10, 69);//设置起点
    
    CGContextAddLineToPoint(context,
                            location  ,  59);
    
    CGContextAddLineToPoint(context,
                            location -10, 69);
    
    CGContextClosePath(context);//路径结束标志，不写默认封闭
    
    [[UIColor whiteColor] setFill];  //设置填充色
    
    [[UIColor whiteColor] setStroke]; //设置边框颜色
    
    CGContextDrawPath(context,
                      kCGPathFillStroke);//绘制路径path
    
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[WBPopMenuSingleton shareManager]hideMenu];
}

@end
